<?php
namespace App\Controllers;
use \interop\Container\ContainerInterface as ContainerInterface;
use App\Models\UserModel;
use pdo;
class UserController extends BaseController{
	public function index(){
        include APP_PATH.'/Views/user/login.html';
	}
	public function login(){
		include APP_PATH.'/Views/user/login.html';
	}
	public function logout($request,$respone,$args){
		unset($_SESSION["loginName"]);
		include APP_PATH.'/Views/user/login.html';
	}
	public function userIndex(){
		include APP_PATH.'/Views/user/user.html';
	}
	public function postError(){
		include APP_PATH.'/Views/user/405.html';
	}
	public function uploadForm(){
		include APP_PATH.'/Views/user/msg/update.html';
	}
	public function postLogin($request,$respone,$args){
		$name = $request->getParam('Email');
	    $pas = $request->getParam('Password');
	    $user=new UserModel();
	    if($user->getUserInfo($name,$pas)){
	    	$data=array('massage'=>'成功','code'=>0);
	    	$_SESSION['loginName']=$name;
	    	$loginName=$_SESSION['loginName'];
		    $ret=$user->getUserName($loginName);
		    $userMsg=json_encode($ret);
		    session_start();
		    $_SESSION['userMsg']=$userMsg;
	    }
	    else{
	    	$data=array('message'=>'用户名或密码不正确','code'=>101);
	    }
	    return $respone->withJson($data);
	    
	}
	public function userSelect($request,$respone,$args){
		   $name=$request->getParam('name');
		   $user=new UserModel();
		   $ret=$user->selectUser($name);
		   $userSelect=json_encode($ret);
           if($user->selectUser($name)){
           	$data=array('massage'=>"$userSelect",'code'=>0);
           }
           else{
	    	$data=array('message'=>'用户名或密码不正确','code'=>101);
	    }
	    return $respone->withJson($data);
	}
	public function getName($request,$respone,$args){
		  $userJson=$_SESSION['userMsg'];
		  return $userJson;
	}
	public function getEmail($request,$respone,$args){
		$email=$request->getParam('email');
		$user=new UserModel();
		$result=json_encode($user->getEmail($email));
		if($user->getEmail($email)){
           	$data=array('massage'=>"$result",'code'=>0);
           }
           else{
	    	$data=array('message'=>'','code'=>101);
	    }
	    return $respone->withJson($data);
		
	}
 	public function userList($request,$respone,$args){
		include APP_PATH.'/Views/user/userMsg.html';
		
	}
	public function userInsertForm(){
		include APP_PATH.'/Views/user/msg/insert.html';
	}
	public function userAdd($request,$respone,$args){
		$user=new UserModel();
		$roleid=$request->getParam('roleid');
		$username=$request->getParam('username');
		$pas=$request->getParam('pas');
		$xingBie=$request->getParam('xingBie');
		$email=$request->getParam('email');
		$phone=$request->getParam('phone');
		$data=array();
		$data['Roleid']=$roleid;
		$data['UserName']=$username;
		$data['Password']=$pas;
		$data['Sex']=$xingBie;
		$data['Email']=$email;
		$data['Phone']=$phone;
		$data['time']=time();
		if($user->add($data)){
			$ret=array('massage'=>"添加成功",'code'=>0);
		}
		else{
			$ret=array('massage'=>"添加失败",'code'=>101);
		}
		return $respone->withstatus(200)->withJson($ret);
	}
	public function userUpdateForm($request,$respone,$args){
	   include APP_PATH.'/Views/user/msg/update.html';
	}
	public function userUpdate($request,$respone,$args){
		$user=new UserModel();
		$id=$request->getParam('id');
		$roleid=$request->getParam('roleid');
		$username=$request->getParam('username');
		$pas=$request->getParam('pas');
		$sex=$request->getParam('sex');
		$email=$request->getParam('email');
		$phone=$request->getParam('phone');
		$data=array();
		$data['Roleid']=$roleid;
		$data['UserName']=$username;
		$data['Password']=$pas;
		$data['Sex']=$sex;
		$data['Email']=$email;
		$data['Phone']=$phone;
		if($user->update($id,$data)){
			$ret=array('massage'=>"修改成功",'code'=>0);
		}
		else{
			$ret=array('massage'=>"修改失败",'code'=>101);
		}
		return $respone->withstatus(200)->withJson($ret);
	}
	public function userDelete($request,$respone,$args){
		$id=$request->getParam('id');
		$user=new UserModel();
		if($user->delete($id)){
			$ret=array('massage'=>"删除成功",'code'=>0);
		}
		else{
			$ret=array('massage'=>"删除失败",'code'=>101);
		}
		return $respone->withstatus(200)->withJson($ret);
	}
	public function userMenu($request,$respone,$args){
		$id=$request->getParam('id');
		$user=new UserModel();
		$result=json_encode($user->userMenuMsg($id));
		if($user->userMenuMsg($id)){
			$ret=array('massage'=>"$result",'code'=>0);
		}
		else{
			$ret=array('massage'=>"修改失败",'code'=>101);
		}
		return $respone->withstatus(200)->withJson($ret);
	}
	public function userInfo($request,$respone,$args){
		include APP_PATH.'/Views/user/userinfo.html';
	}
	public function getInfo($request,$respone,$args){
		$id=$request->getParam('id');
		$user=new UserModel();
		$result=json_encode($user->select($id));
		if($user->select($id)){
			$ret=array('massage'=>"$result",'code'=>0);
		}
		else{
			$ret=array('massage'=>"修改失败",'code'=>101);
		}
		return $respone->withstatus(200)->withJson($ret);
	}
	public function userItem($request,$respone,$args){
		$user=new UserModel();
		$order=array();
		$order['ID']="id DESC";
		$draw = $request->getParam('draw');
        $order_column = $request->getParam("['order']['0']['column']"); 
        $order_dir= $request->getParam("['order']['0']['dir'];");
        $orderSql = "";
        if(isset($order_column)){
        $i = intval($order_column);
        switch($i){
        case 0;$orderSql = " order by id ".$order_dir;break;
        case 1;$orderSql = " order by name ".$order_dir;break;
        case 2;$orderSql = " order by age ".$order_dir;break;
        case 3;$orderSql = " order by sex ".$order_dir;break;
        case 4;$orderSql = " order by email ".$order_dir;break;
        case 4;$orderSql = " order by phone ".$order_dir;break;
        default;$orderSql = '';
    }
    }

   $start = $request->getParam('start');
   $length = $request->getParam('length');
   $user->order($order);
   $result=$user->selectAll();
   $recordsTotal = 0;
   $recordsTotal = count($result);
   json_encode($result);
   $index=0;
   if($recordsTotal%$length==0){
   	    $recordsFiltered=($recordsTotal/$length)*10;
   }
   else{
   	    $recordsFiltered=($recordsTotal/$length)*10+1;
   }
     for($i=$start;$i<$start+$length;$i++,$index++)
   	{if(empty($result[$i])){
   		break;
   	}
   	$infos[$index]=$result[$i];
    }
    for($i=0,$index1=$start+1;$i<count($infos);$i++,$index1++){
		    	$infos[$i]['Index']=$index1;
	}
    echo json_encode(array(
	"draw" => $draw,
	"recordsTotal" =>$recordsTotal,
	"recordsFiltered" =>$recordsFiltered,  // necessary
	"data" =>$infos // necessary
	));
	}
}